package main

import (
	"context"
	"fmt"
	greeter "go_final/demos/grpc_metadata/protofile"
	"go_final/demos/jeager_grpc_integration/otgrpc"

	"github.com/opentracing/opentracing-go"

	"github.com/uber/jaeger-client-go"
	jaegercfg "github.com/uber/jaeger-client-go/config"

	"google.golang.org/grpc"
)

func main() {
	cfg := jaegercfg.Configuration{
		Sampler: &jaegercfg.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &jaegercfg.ReporterConfig{
			LogSpans:           true,
			LocalAgentHostPort: "192.168.4.170:6831",
			//CollectorEndpoint: "http://120.24.67.46:14268/api/traces",
		},
		ServiceName: "mxshop",
	}

	tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
	if err != nil {
		fmt.Println("出错了")
		panic(err)
	}
	// 将tracer设置成全局的tracer, 设置完成以后即可通过opentracing.StartSpan()来调用, 也可以通过opentracing.GlobalTracer()来获取这个对象
	opentracing.SetGlobalTracer(tracer)
	defer closer.Close()

	conn, err := grpc.Dial(":50051", grpc.WithInsecure(), grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())))
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	c := greeter.NewGreeterClient(conn)

	res, err := c.SayHi(
		context.Background(),
		&greeter.SayHiRequest{
			Id: "Felix",
		},
	)
	if err != nil {
		fmt.Println("err: ", err)
		return
	}

	fmt.Println("res", res)
}
